<?php

abstract class Core_BaseController
{
    private $data = array();
    /**
     * @var Core_Registry
     */
    private $registry = null;
    /**
     * @var Twig_Environment
     */
    protected  $twig;
    /**
     * @var DataModule
     */
    protected $dm;
    /**
     * @var Auth_Auth2
     */
    protected $auth;

      protected  function array_push_assoc($array, $key, $value){
$array[$key] = $value;
return $array;
}
    protected function vardump($message = "",$value){

        echo "<pre><h3>$message:</h3></br>";
        var_dump($value);
        echo "</pre>";
    }
    protected function __call($name, $arguments)
    {


    }


    abstract function indexAction();

    public function __construct()
    {


    }

    public function getRegistry()
    {
        return $this->registry;

    }

    public function setRegistry($registry)
    {
        $this->registry = $registry;

    }

    /**
     * @param \Auth_Auth2 $auth
     */
    public function setAuth($auth)
    {
        $this->auth = $auth;
    }

    /**
     * @return \Auth_Auth2
     */
    public function getAuth()
    {
        return $this->getRegistry()->auth;
    }

    /**
     * @param \DataModule $dm
     */
    public function setDm($dm)
    {
        $this->dm = $dm;
    }

    /**
     * @return \DataModule
     */
    public function getDm()
    {
        return $this->getRegistry()->dm;
    }

    /**
     * @param \Twig_Environment $twig
     */
    public function setTwig($twig)
    {
        $this->twig = $twig;
    }

    /**
     * @return \Twig_Environment
     */
    public function getTwig()
    {
        return $this->twig;
    }

    public function userHasPermission($actionPermissions){
        //echo "userHasPermission()<br/>";
        $status = false; // -1 Deny 1 allow
        $authorized = $this->checkAuth();
         //var_dump($authorized);
        if(isset($authorized['auth'])){
           //$this->vardump($authorized);
            $userPermission = $this->getAuth()->getUserRoles($authorized['name']);
            ///echo "user permission is $userPermission <br/>";
            $userPermissions[] = $userPermission;
           // var_dump($userPermission);
            //var_dump($actionPermissions);
            $k = count(array_intersect($actionPermissions,$userPermissions));

            if($k > 0){
              $status = true;
            }
            else
                $status = false;
        } else $status = false;

         //var_dump($status);
        return $status;


    }
    public function getUserAndRole($actionPermission){




    }

        public function checkAuth()
    {
        $auth = $this->getAuth();
        $dm = $this->getDm();
        $auth->setDataModule($dm);
        $isAuthorized = $auth->isAuthorizedUserVer2();
          if ($isAuthorized != -1) {
            $auth = array('auth' => true,
                          'name' => $isAuthorized);
            return $auth;
        } else {
            $auth = array('auth' => false,
                           'name' => 'guest');
            return $auth;
        }
    }
}